import { ICatList, IData, IAction } from "./interface";
import qs from "qs";
import axios from "axios";
const catList: ICatList = {
  namespaced: true,
  state: {
    loading: false,
    data: [],
  },
  actions: {
    async gethot({ commit }, { cid }) {
      const res: IData[] = await axios(
        `/index.php?r=class%2Fajaxsub&page=1&cid=${cid}&px=t&cac_id=`
      ).then((res): IData[] => {
        console.log(res.data.data);
        return res.data.data.goodsList;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    async getlatest({ commit }, { cid }) {
      const res: IData[] = await axios(
        `/index.php?r=class%2Fajaxsub&page=1&cid=${cid}&px=latest&cac_id=`
      ).then((res): IData[] => {
        console.log(res.data.data);
        return res.data.data.goodsList;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    async getsell({ commit }, { cid }) {
      const res: IData[] = await axios(
        `/index.php?r=class%2Fajaxsub&page=1&cid=${cid}&px=sell&cac_id=`
      ).then((res): IData[] => {
        console.log(res.data.data);
        return res.data.data.goodsList;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    async getprice({ commit }, { cid }) {
      const res: IData[] = await axios(
        `/index.php?r=class%2Fajaxsub&page=1&cid=116908&px=price_h&cac_id=`
      ).then((res): IData[] => {
        console.log(res.data.data);
        return res.data.data.goodsList;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    setload({ commit }) {
      const action: { type: string } = {
        type: "SETLOAD",
      };
      commit(action);
    },
  },
  mutations: {
    SETDATA(state, action) {
      state.data = action.payload;
      state.loading = false;
    },
    SETLOAD(state) {
      state.loading = true;
    },
  },
};
export default catList;
